#!/bin/bash

echo "=== 测试商品新增API ==="

# 1. 登录获取token
echo "1. 登录获取token..."
LOGIN_RESPONSE=$(curl -s -X POST "http://localhost:8002/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"123456"}')

echo "登录响应: $LOGIN_RESPONSE"

# 提取token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
echo "获取到token: $TOKEN"

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败，无法获取token"
    exit 1
fi

# 2. 测试新增商品
echo "\n2. 测试新增商品..."
CREATE_RESPONSE=$(curl -s -X POST "http://localhost:8002/api/v1/goods" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "name": "测试商品",
    "description": "这是一个测试商品",
    "image": "/uploads/test.jpg",
    "points": 200,
    "stock": 10,
    "status": 1
  }')

echo "新增商品响应: $CREATE_RESPONSE"

# 检查响应
if echo "$CREATE_RESPONSE" | grep -q '"code":200'; then
    echo "\n✅ 商品新增测试成功！"
else
    echo "\n❌ 商品新增测试失败！"
fi